//
//  AUAddressPickerVC.swift
//  CreditLoans
//
//  Created by 谢强 on 2017/12/8.
//  Copyright © 2017年 com.ucfpay. All rights reserved.
//

import UIKit

import AwesomeUBaseModule

// MARK:- <常量>
private let au_kTitleViewHeight: CGFloat = 85
private let au_kTitleViewTopMargin: CGFloat = 10

typealias AUAddressPickerCallback = () -> ()

public class AUAddressPickerVC: UIViewController, AUAddressPickerProtocol {
    
    // MARK:- <属性>
    private let pickerView = UIView()
    
    fileprivate lazy var titleView: AUAddressPickerTitleView = {
        let frame = CGRect(origin: .zero, size: CGSize(width: au_kScreenWidth, height: 80 * au_kScreenScale))
        let titleView = AUAddressPickerTitleView(frame: frame, titles: ["请选择"])
        titleView.delegate = self
        return titleView
    }()
    
    fileprivate lazy var contentView: AUAddressPickerContentView = {[weak self] in
        
        // 设置frame
        let topY = titleView.frame.maxY
        let contentViewFrame = CGRect(x: 0, y: 0, width: au_kScreenWidth, height: 310 * au_kScreenScale)
        
        // 创建子控制器
        var contentView = AUAddressPickerContentView(
            frame: contentViewFrame,
            childViewControllers: nil,
            parrentViewController: self,
            scrollEnable: true)
        contentView.delegate = self
        contentView.completionCallback = { [weak self] in
            self?.pickerDismiss()
//            self?.dismiss(animated: true, completion: {
//                self?.pickerCompletionCallback?(self!.titleView.address)
//            })
        }
        
        return contentView
    }()
    
    public var pickerCompletionCallback: ((_ address: String) -> ())?

    // MARK:- <系统>
    override public func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0, alpha: 0.7)
        
        // 添加contentView
        view.addSubview(pickerView)
        pickerView.addSubview(titleView)
        pickerView.addSubview(contentView)
        
        pickerView.frame = CGRect(x: 0, y: view.frame.height, width: view.frame.width, height: 390 * au_kScreenScale)
        
        contentView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(0)
            make.height.equalTo(310 * au_kScreenScale)
        }
        
        titleView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(contentView.snp.top)
            make.height.equalTo(80 * au_kScreenScale)
        }
        
        titleView.closeButtonActionCallback = { [weak self] in
            self?.close()
        }
    }
    
    override public func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if pickerView.frame.origin.y == view.frame.height {
            UIView.animate(withDuration: 0.3) {
                self.pickerView.frame.origin.y = self.view.frame.height - self.pickerView.frame.height
            }
        }
    }
    
    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        close()
    }

    deinit {
        print("地区选择器销毁")
    }
}

extension AUAddressPickerVC {
    
    func close() {
        UIView.animate(withDuration: 0.3, animations: {
            self.pickerView.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: 390 * au_kScreenScale)
        }) { (_) in
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    func pickerDismiss() {
        UIView.animate(withDuration: 0.3, animations: {
            self.pickerView.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: 390 * au_kScreenScale)
        }) { (_) in
            self.dismiss(animated: true, completion: {
                self.pickerCompletionCallback?(self.titleView.address)
            })
        }
    }
}

// MARK:- <AUAddressPickerTitleViewDelegate>
extension AUAddressPickerVC: AUAddressPickerTitleViewDelegate {
    func pageTitleView(_ titleView: AUAddressPickerTitleView, clickedIndex index: Int) {
        
        contentView.setPage(currentIndex: index, animated: true)
    }
}

// MARK:- <XQPageContenViewDelegate>
extension AUAddressPickerVC: AUAddressPickerContentViewDelegate {
    func pageContentView(_ pageContentView: AUAddressPickerContentView,
                         progress: CGFloat,
                         sourceIndex: Int,
                         targetIdex: Int,
                         address: String) {
        titleView.setProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIdex, address: address)
    }
    
    func updateTitle(_ pageContentView: AUAddressPickerContentView, sourceIndex: Int, address: String) {
        titleView.updateTitle(sourceIndex, address: address)
    }
}

